import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== pages
const AddDicModal = $Lazy.state(()=>import('#private/pages/add-dic-modal'))
// ===================================================================== table
const UnitConversionTable 			= $Lazy.load(()=>import('./table/unit-conversion'))
const SuggestionsExplanationsTable 	= $Lazy.load(()=>import('./table/suggestions-explanations'))
const ReportResultDictionaryTable 	= $Lazy.load(()=>import('./table/report-result-dictionary'))
const ResultConversionRuleTable		= $Lazy.load(()=>import('./table/result-conversion-rule'))
// ===================================================================== data
const checkbox = { width:280, type:'checkbox', after:true, mb:0, value:0 }
// ===================================================================== component
class Index extends React.Component{
	state = {
		// 提交表单列表
		formData : [
			{ label:' ', tag: '需要手动验证发布',		name: 'is_user_verify_push', 	 	...checkbox },
			{ label:' ', tag: '回传结果自动批准',		name: 'return_result_auto', 		...checkbox },
			{ label:' ', tag: '打印配送',				name: 'is_print_delivery', 			...checkbox },
			{ label:' ', tag: '是否采集图片',			name: 'is_coll_pic', 				...checkbox },
			{ label:' ', tag: '二次报告',				name: 'second_report', 				...checkbox },
			{ label:' ', tag: '结果转换规则',			name: 'is_result_conv_rule', 		...checkbox, onValue:({value, ref})=>{
				/* 2022-01-20 结果转换规则与报告结果只能选一个  --j+2 */
				// if(value){
				// 	ref.setValue({ result1:0 })
				// }
			} },
			{ label:' ', tag: '是否上传报告单',		name: 'is_upload_report_form', 		...checkbox },
			{ label:' ', tag: '危急值不出报告',		name: 'is_critical_report', 		...checkbox },
			{ label:' ', tag: '阳性不出报告',			name: 'positive_report', 		...checkbox, mb:10 },
			/* 2021-7-19  添加报告单外网不显示 --马婷 */
			{ label:' ', tag: '报告单外网不显示',		name: 'is_show_out', 				...checkbox },
			{ label:' ', tag: '是否回传参考值',		name: 'is_return_range', 			...checkbox },
			{ label:' ', tag: '是否回传单位',			name: 'is_return_unit', 			...checkbox },
			{ label: '是否启用多结果',		tag:'报告结果', name: 'result1', type:'checkbox', br:true, onValue:({value, ref})=>{
				/* 2022-01-20 结果转换规则与报告结果只能选一个  --j+2 */
				// if(value){
				// 	ref.setValue({ is_result_conv_rule:0 })
				// }
			}},
			{ tag:'结果2', name: 'result2',	type:'checkbox', after:true },
			{ tag:'结果3', name: 'result3', type:'checkbox', after:true },
			{ label: '报告单模板',			name: 'report_tpl', 	half:true, required:true, type:'select', loading:true, br:true },
			{ label: '报告结果小数点位数',	name: 'decimal_length', half:true, type:'number', required:true, value: 0 },
			
			{ label: '结果计算公式',			name: 'result_formula',  	half:true, title:'结果计算' },
			{ label: '结果类型',				name: 'result_type',  		width:'25%', required:true, type:'select', loading:true, idStr:'code', onValue:({value, formData})=>{
				$fn.hideRows(formData, 'result_dis_code', +value !== 1203)
				this.setState({ formData })
			} },
			{ label: '结果提示类型',			name: 'result_tips_temp',  	width:'25%', type:'select', loading:true, idStr:'uuid', nameStr:'temp_name' },
			{ label: '所属字典分类',			name: 'result_dis_code',  	half:true, type:'select', loading:true, hide:true },
			
			{ label: '保密级别',				name: 'secret_level',  		half:true,  type:'select', loading:true },
			{ label: '通用结果单位',			name: 'result_unit',  		half:true,  type:'select', loading:true, idStr:'code',btn: { label:'添加', onClick:()=>{
			  	this.dicRef.open('通用结果单位', 'result_unit', 18000)
			}} },
			{ label: '临床可报告范围',		names: ['clinical_range','clinical_range_end'],  	half:true, type:'range' },
			{ label: '上级质控',				name: 'superiorquality',  							half:true, suffix:'%' },
			{ label: '线性范围',				names: ['linear_range','linear_range_end'],  		half:true, type:'range' },
			{ label: '结果默认值',			name: 'result_default_value',  						half:true },
			
			{ label:' ', tag: '特殊打印纸',	name: 'is_printing', 		full:true, type:'checkbox', value:0,  title:'打印设置', onValue:option => $fn.disabledToRows.call(this, option, 'spec_print_paper') },
			{ label: '打印纸张',				name: 'spec_print_paper', 	half:true, type:'select', loading:true, disabled:true, idStr:'code' },
			{ label: '打印份数',			name: 'print_num', 			half:true, type:'number', value:0 },
		],
	}
	
	componentDidMount(){
		this.submit = { kind_id: this.props.kind_id }
		this.getSelect()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 报告单模板
			$fn.setRow( formData, 'report_tpl',  await f.getCache({ api: 'report_template' }) )
			// 字典
			const dic = await f.getDicCache({ code: [ 1200, 18000, 38000 ]})
			// 结果类型
			$fn.setRow( formData, 'result_type',  dic['1200'] )
			// 结果单位
			const result_unit = $fn.setRow( formData, 'result_unit',  dic['18000'] )
			// 特殊打印纸
			$fn.setRow( formData, 'spec_print_paper',  dic['38000'] )
			// 结果提示类型
			$fn.setRow( formData, 'result_tips_temp',  await f.getCache({ api: 'result_tips_type' }) )
			// 保密级别
			$fn.setRow( formData, 'secret_level',  await f.secret_level )
			// 所属字典分类
			$fn.setRow( formData, 'result_dis_code', await f.getCache({ api:'dictionary/select' }))
			//
			this.fetch()
			//
			this.setState({ formData, result_unit })
		})
	}
	
	onSubmit = () => this.formRef.submit()
	
	fetch = () => $http.submit(null,'check-result-cats/info', { param:{uuid:this.props.kind_id}, isPull:true } ).then(data=>{
		this.initSubmit = data
		
		const { formData } = this.state
		$fn.hideRows(formData, 'result_dis_code', +data?.result_type !== 1203)
		this.setState({ formData }, ()=>{
			this.formRef.setValue({spec_print_paper: '38004', ...data })
		})
	})
	
	render(){
		const { edit } = this.props
		return (
			<>
				<Box contentClassName='pt10 pr10 pl10' first>
					{ 
						SubmitForm.call(this,{
							msg:'保存', labelWidth:'s11', api:'check-result-cats/add',
							onOk:this.props.onAudit,
							disabled:edit,
							onForm: this.getSelect
						}) 
					}
				</Box>
				{/* 新增字典 */}
				<AddDicModal onRef={ref => this.dicRef = ref} top={this} disabled={edit} />
				{/* 单位换算系数 */}
				<UnitConversionTable {...this.props} data={this.state.result_unit} disabled={edit} />
				{/* 建议与解释 */}
				<SuggestionsExplanationsTable {...this.props} disabled={edit} />
				{/* 报告结果字典 */}
				<ReportResultDictionaryTable {...this.props} disabled={edit} />
				{/* 结果转换规则 */}
				<ResultConversionRuleTable {...this.props} disabled={edit} />
			</>
		)
	}
}

export default Index